package com.spring.controller.javastack.bean;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * Created by chenhs on 2018/6/14.
 *
 * 从测试结果看出来的结果可以总结初始和销毁对应的顺序为：
 * 初始：类构造器 > @PostConstruct > InitializingBean > init-method
 * 销毁：@PreDestroy > DisposableBean > destroyMethod
 *
 ===Constructor===
 ===@PostConstruct===
 === InitializingBean afterPropertiesSet ===
 ===initMethod===
 ===@PreDestroy===
 === DisposableBean destroy ===
 ===destroyMethod===
 *
 */
public class UserService implements InitializingBean, DisposableBean {

    public UserService(){
        System.out.println("===Constructor===");
    }


    @PostConstruct
    public void postConstructor() {
        System.out.println("===@PostConstruct===");
    }

    @PreDestroy
    public void preDestroy() {
        System.out.println("===@PreDestroy===");
    }

    public void initMethod(){
        System.out.println("===initMethod===");
    }

    public void destroyMethod() throws Exception {
        System.out.println("===destroyMethod===");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("=== InitializingBean afterPropertiesSet ===");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("=== DisposableBean destroy ===");
    }
}
